package com.wyr.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyr.admin.service.UserInfoService;
import com.wyr.common.vo.Result;
import com.wyr.dao.entity.SysUser;
import com.wyr.dao.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangyiren
 * @version 1.0
 * &#064;title UserInfoServiceImpl
 * &#064;description
 * &#064;date 2025/03/21 10:02
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 10:06
     */
    @Override
    public Result<?> userLogin(String username, String password) {
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            Map<String, Object> data = new HashMap<>(2);
            data.put("token", StpUtil.getTokenValue());
            return Result.ok(data);
        }
        return Result.error("账号密码不正确");
    }

    /**
     * 分页查询用户
     *
     * @param pageNum
     * @param pageSize
     * @param name     &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 10:04
     */
    @Override
    public Result<?> listUserByPage(int pageNum, int pageSize, String name) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), SysUser::getNickName, name);
        wrapper.orderByDesc(SysUser::getCreateTime);
        Page<SysUser> userPage = sysUserService.page(page, wrapper);
        return Result.ok(userPage);

    }

    /**
     * 新增用户
     *
     * @param user &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 10:03
     */
    @Override
    public Result<?> saveUser(SysUser user) {
        boolean save = sysUserService.save(user);
        return Result.ok(save);
    }
}
